#!/usr/bin/env python
# -*- coding:utf-8 -*-
import io  
import sys
from urllib import request
from bs4 import BeautifulSoup
import re
import pymysql
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')  
resp = request.urlopen("http://news.baidu.com/").read().decode("utf-8")
soup =BeautifulSoup(resp,"html.parser")
listUrls=soup.find_all("a",href=re.compile(".*\/\/news\.baidu.*"))
#for url in listUrls:
    #print (url.get_text(),"<------------>"+url["href"])
# 数据库操作
# 创建连接
conn = pymysql.connect(host='115.28.54.229', port=3306, user='root', passwd='root', db='python_study', charset='utf8')
# 创建游标
cursor = conn.cursor()
# 执行SQL，并返回收影响行数
# effect_row = cursor.execute("select * from test")
# row_all = cursor.fetchall()
sqls="insert into `test` (`name`,`url`) values (%s,%s)"

for url in listUrls:
    cursor.execute(sqls,(url.get_text(),url["href"]))
conn.commit()
conn.close()
     